<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2021-04-27
 * Time: 21:54
 */
namespace app\admin\controller\command;
use app\admin\model\traits\TablHandle;
use app\BaseController;
use think\App;

class AdminController extends BaseController
{
    public $AllData = [];
    protected $middleware = [
        MyMiddle::class
    ];
    public function __construct(App $app)
    {
        parent::__construct($app);
        $this->assign('adminuser',session('adminuser'));
    }

    /**获取成功
     * @param string $msg
     * @param int $code
     * @param array $data
     * @param null $action
     * @return \think\response\Json|\think\response\View
     */
    public function success($msg='成功',$data=[],$action=null,$code=200){
        if($action){
            $action = url($action)->build();
        }
        if(\request()->isAjax()){
            return json_to_respos($msg,$code,$data);
        }
        return view('index/exceptionpage',['code'=>$code,'action'=>$action,'msg'=>$msg]);
    }

    /**处理获取数据的不同
     * @param array $data
     * @return array
     */
    public function arrangementData(array $data = []){
//        echo '<pre>';
////        print_r($data);
//        print_r($this->AllData);
//        exit;
        foreach ($data as $key=>$value){
            foreach ($this->AllData['field'] as $k=>$v){
                if(isset($v['show'])){
                    if($v['show']!=1){
                        continue;
                    }
                }else{
                    continue;
                }
                if(!isset($v['formtype'])){
                    continue;
                }
                if(in_array($v['formtype'],['radio','checkbox','select'])){
                    $color = [
                        'green',
                        'red',
                        'orange',
                        '#9393FF',
                        'purple',
                        '#AE0000',
                        '#004B97',
                        '#28FF28',
                        '#9393FF',
                        '#737300',
                        '#FFAD86',
                        '#AE57A4',
                        '#5151A2',
                        '#921AFF',
                        '#5B00AE',
                        '#4D0000',
                        '#000093',
                        '#02F78E',
                        '#FFA042',
                    ];
//                    echo '<pre>';
//                    print_r($value);
//                    print_r($v);
//                    exit;
                    if(isset($v['option'])&&!empty($v['option'])){
                        $num = 0;
                        foreach ($v['option'] as $ks=>$s){
                            if($ks==$value[$v['field']]){
                                break;
                            }
                            $num++;
                        }
//                        $v['option'] = TablHandle::strToArray($v['option']);
                        if(isset($value[$v['field']])){
                            try{
                                $value[$v['field']] = '<span style="color: '.(isset($color[$num])?$color[$num]:'green').'">'.$v['option'][$value[$v['field']]].'</span>';
                            }catch (\Exception $e){
//                                echo '<pre>';
//                                print_r($value[$v['field']]);
//                                print_r($v['option']);
//                                exit;
                            }

                        }
                    }
                }elseif ($v['formtype']=='datatime'){
                    //处理数字
                    if(is_numeric($value[$v['field']])){
                        $value[$v['field']] = date('Y-m-d H:i:s',$value[$v['field']]);
                    }
                }elseif ($v['formtype']=='icon'){
                    if(isset($value[$v['field']])&&$value[$v['field']]){
                        $value[$v['field']] = '<i class="'.$value[$v['field']].'"></i>';
                    }
                }
                $data[$key] = $value;
            }
        }
        return $data;
    }

    /**提交数据转化
     * @return mixed
     */
    public function SubmitDataArray(){
        $data = request()->param();
        $field = $this->AllData['field'];
        foreach ($field as $key=>$value){
            //处理开关
            if($value['formtype']=='switch'){
                if(isset($data[$key])&&$data[$key]=='on'){
                    $data[$key] = 1;
                }else{
                    $data[$key] = 0;
                }
            }
            //处理时间
            if($value['formtype']=='datatime'){
                if(isset($data[$key])&&$data[$key]){
                    $data[$key] = strtotime($data[$key]);
                }else{
                    $data[$key] = 0;
                }
            }
            if($value['formtype']=='icon'){
                if(isset($data[$key])&&$data[$key]){
                    $data[$key] = 'fa '.$data[$key];
                }else{
                    $data[$key] = '';
                }
            }
        }
        return $data;
    }

    /**获取失败
     * @param string $msg
     * @param int $code
     * @param array $data
     * @param null $action
     * @return \think\response\Json|\think\response\View
     */
    public function error($msg='失败',$code=202,$data=[],$action=null){
        if($action){
            $action = url($action)->build();
        }
        if(\request()->isAjax()){
            return json_to_respos($msg,$code,$data);
        }
        return view('index/exceptionpage',['code'=>$code,'action'=>$action,'msg'=>$msg]);
    }
    /**
     * 模板变量赋值
     * @param string|array $name 模板变量
     * @param mixed $value 变量值
     * @return mixed
     */
    public function assign($name, $value = null)
    {
        return $this->app->view->assign($name, $value);
    }

    /**
     * 解析和获取模板内容 用于输出
     * @param string $template
     * @param array $vars
     * @return mixed
     */
    public function fetch($template = '', $vars = [])
    {
        return $this->app->view->fetch($template, $vars);
    }

    /**
     * 重写验证规则
     * @param array $data
     * @param array|string $validate
     * @param array $message
     * @param bool $batch
     * @return array|bool|string|true
     */
    public function validate(array $data, $validate, array $message = [], bool $batch = false)
    {
        try {
            parent::validate($data, $validate, $message, $batch);
        } catch (\Exception $e) {
            $this->error($e->getMessage());
        }
        return true;
    }
    /**
     * 构建请求参数
     * @param array $excludeFields 忽略构建搜索的字段
     * @return array
     */
    protected function buildTableParames($excludeFields = [])
    {
        $searchParams = request()->param('searchParams',[]);
//        echo '<pre>';
//        print_r($searchParams);
//        exit;
        $where = [];
        if(!is_array($searchParams)){
            $searchParams = json_decode($searchParams,true);
        }
        $field = $this->AllData['field'];
            foreach ($searchParams as $key=>$value){
                if(!in_array($key,$excludeFields)){
                    if(!empty($value)||$value===0){
                        if(isset($field[$key]['formtype'])&&($field[$key]['formtype']=='input'||$field[$key]['formtype']=='')){
                            $where[] = [$key,'like',"%".$value."%"];
                        }elseif (isset($field[$key]['formtype'])&&in_array($field[$key]['formtype'],['optgroup','select','radio'])){
                            $where[] = [$key,'=',$value];
                        }
                    }
                }
            }
        return $where;
    }
}